package com.coldworks.coldjoke.adapter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.coldworks.base.manager.BaseStorageManager;
import com.coldworks.coldjoke.R;
import com.coldworks.coldjoke.manager.JokeManager;
import com.coldworks.coldjoke.model.JokeModel;
import com.coldworks.coldjoke.task.AsyncImgLoader;
import com.coldworks.coldjoke.task.AsyncImgLoader.UrlImageCallback;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.XmlResourceParser;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class JokeListViewAdapter extends BaseAdapter {

	private JokeManager listManager;
	private Context context;
	private int selectItem = -1;
	private XmlResourceParser xrp1, xrp3;
	private ColorStateList csl1, csl3;
	protected final Logger log = LoggerFactory.getLogger(this.getClass());

	public JokeListViewAdapter(Context context, JokeManager listManager) {
		this.listManager = listManager;
		this.context = context;
		try {
			xrp1 = context.getResources().getXml(R.color.selector_text_color_1);
			csl1 = ColorStateList.createFromXml(context.getResources(), xrp1);
			xrp3 = context.getResources().getXml(R.color.selector_text_color_3);
			csl3 = ColorStateList.createFromXml(context.getResources(), xrp3);
		} catch (Exception e) {
			log.info("", e);
		}

	}

	@Override
	public int getCount() {
		if (listManager == null) {
			return 0;
		}
		return listManager.getListShow().size();
	}

	@Override
	public JokeModel getItem(int position) {
		return listManager.getListShow().get(position);
	}

	public void setSelectItem(int selectItem) {
		this.selectItem = selectItem;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		JokeViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(this.context).inflate(
					R.layout.navigation_joke_listview_item, parent, false);
			holder = new JokeViewHolder();
			holder.jokeText = (TextView) convertView
					.findViewById(R.id.joke_summary);
			holder.imageView = (ImageView) convertView
					.findViewById(R.id.joke_image);
			holder.jokeLikeNum = (TextView) convertView
					.findViewById(R.id.joke_like_num);
			holder.jokeDisLikeNum = (TextView) convertView
					.findViewById(R.id.joke_dislike_num);
			holder.jokeCommentNum = (TextView) convertView
					.findViewById(R.id.joke_comment_num);
			convertView.setTag(holder);
		} else {
			holder = (JokeViewHolder) convertView.getTag();
		}

		if (position == selectItem) {
			// convertView.setBackgroundDrawable(context.getResources()
			// .getDrawable(R.drawable.joke_listview_item_bg_pressed));
			holder.jokeText.setTextColor(context.getResources().getColor(
					R.color.white));
			holder.jokeLikeNum.setTextColor(context.getResources().getColor(
					R.color.white));
			holder.jokeDisLikeNum.setTextColor(context.getResources().getColor(
					R.color.white));
			holder.jokeCommentNum.setTextColor(context.getResources().getColor(
					R.color.white));
		} else {
			holder.jokeText.setTextColor(csl1);
			holder.jokeLikeNum.setTextColor(csl3);
			holder.jokeDisLikeNum.setTextColor(csl3);
			holder.jokeCommentNum.setTextColor(csl3);
			// convertView.setBackgroundDrawable(context.getResources()
			// .getDrawable(R.drawable.selector_listview_item_bg_joke));
		}

		JokeModel jokeInfo = listManager.getListShow().get(position);
		String imgUrl = jokeInfo.getImgUrl();
		if (!imgUrl.equals("")) {
			holder.imageView.setVisibility(View.VISIBLE);
			Bitmap cachedImage = AsyncImgLoader.getInstance().loadDrawableByUrl(
					context, imgUrl, holder.imageView, new UrlImageCallback() {
						@Override
						public void imageLoaded(Bitmap imageDrawable,
								ImageView imageView, String imageUrl) {
							imageView.setImageBitmap(imageDrawable);
						}
					}, BaseStorageManager.getInstance().getImgDir(), false);
			if (cachedImage != null) {
				holder.imageView.setImageBitmap(cachedImage);
			} else {
				holder.imageView.setBackgroundResource(R.drawable.ic_photo);
			}
		} else {
			holder.imageView.setVisibility(View.GONE);
		}

		holder.jokeText.setText(jokeInfo.getJokeText());
		holder.jokeLikeNum.setText(String.valueOf(jokeInfo.getGoodNum()));
		holder.jokeDisLikeNum.setText(String.valueOf(jokeInfo.getBadNum()));
		holder.jokeCommentNum.setText(String.valueOf(jokeInfo.getReplyNum()));

		return convertView;
	}

	static class JokeViewHolder {
		TextView jokeText;
		ImageView imageView;
		TextView jokeLikeNum;
		TextView jokeDisLikeNum;
		TextView jokeCommentNum;
	}
}
